package com.example.asus.yuejian_10.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.RetrofitSeveice;
import com.example.asus.yuejian_10.adapter.ClubclassAdapter;
import com.example.asus.yuejian_10.bean.CCourse;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.asus.yuejian_10.base.BaseActivity.clubid;


/**
 * A simple {@link Fragment} subclass.
 */
public class ClubClassFragment extends Fragment {
    private ClubclassAdapter adapter;
    private RecyclerView rv;
    private RetrofitSeveice service;
    private TextView class_null;

    public ClubClassFragment() {
        // Required empty public constructor
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.URL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        service = retrofit.create(RetrofitSeveice.class);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        class_null= (TextView) view.findViewById(R.id.class_null);
        rv= (RecyclerView) view.findViewById(R.id.rv_club_class);
        Toast.makeText(getActivity(), clubid, Toast.LENGTH_SHORT).show();
        Call<CCourse> call = service.getclass("2ul9sj8vlfoZ122e1s98048k2","1",clubid,"Clubcourse"	);
        call.enqueue(new Callback<CCourse>() {
            @Override
            public void onResponse(Call<CCourse> call, Response<CCourse> response) {
                Toast.makeText(getActivity(), clubid, Toast.LENGTH_SHORT).show();
                if(getActivity()!=null){

                    CCourse clublist = response.body();
                    List<CCourse.ClubcourseEntity> vlist=clublist.getClubcourse();

                    if (vlist.size()==0){
                            Log.v("lkkk","null");
                            class_null.setText("暂时没有课程");
                        }
                    else {
                            adapter = new ClubclassAdapter(getActivity(),vlist);

                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

                            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

                            rv.setLayoutManager(linearLayoutManager);
                            rv.setAdapter(adapter);
                            rv.setItemAnimator(new DefaultItemAnimator());
                        }



                }
            }

            @Override
            public void onFailure(Call<CCourse> call, Throwable t) {
                Toast.makeText(getActivity(), "lianwangshiba", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_club_class, container, false);
    }

}
